
#include "barcode.h"

struct IntString
{
    int ch;
    QByteArray psz;
};

IntString infs[]=
{
    {0,		"bbsbbssbbss"},
    {1,		"bbssbbsbbss"},
    {2,		"bbssbbssbbs"},
    {3,		"bssbssbbsss"},
    {4,		"bssbsssbbss"},
    {5,		"bsssbssbbss"},
    {6,		"bssbbssbsss"},
    {7,		"bssbbsssbss"},
    {8,		"bsssbbssbss"},
    {9,		"bbssbssbsss"},
    {10,	"bbssbsssbss"},
    {11,	"bbsssbssbss"},
    {12,	"bsbbssbbbss"},
    {13,	"bssbbsbbbss"},
    {14,	"bssbbssbbbs"},
    {15,	"bsbbbssbbss"},
    {16,	"bssbbbsbbss"},
    {17,	"bssbbbssbbs"},
    {18,	"bbssbbbssbs"},
    {19,	"bbssbsbbbss"},
    {20,	"bbssbssbbbs"},
    {21,	"bbsbbbssbss"},
    {22,	"bbssbbbsbss"},
    {23,	"bbbsbbsbbbs"},
    {24,	"bbbsbssbbss"},
    {25,	"bbbssbsbbss"},
    {26,	"bbbssbssbbs"},
    {27,	"bbbsbbssbss"},
    {28,	"bbbssbbsbss"},
    {29,	"bbbssbbssbs"},
    {30,	"bbsbbsbbsss"},
    {31,	"bbsbbsssbbs"},
    {32,	"bbsssbbsbbs"},
    {33,	"bsbsssbbsss"},
    {34,	"bsssbsbbsss"},
    {35,	"bsssbsssbbs"},
    {36,	"bsbbsssbsss"},
    {37,	"bsssbbsbsss"},
    {38,	"bsssbbsssbs"},
    {39,	"bbsbsssbsss"},
    {40,	"bbsssbsbsss"},
    {41,	"bbsssbsssbs"},
    {42,	"bsbbsbbbsss"},
    {43,	"bsbbsssbbbs"},
    {44,	"bsssbbsbbbs"},
    {45,	"bsbbbsbbsss"},
    {46,	"bsbbbsssbbs"},
    {47,	"bsssbbbsbbs"},
    {48,	"bbbsbbbsbbs"},
    {49,	"bbsbsssbbbs"},
    {50,	"bbsssbsbbbs"},
    {51,	"bbsbbbsbsss"},
    {52,	"bbsbbbsssbs"},
    {53,	"bbsbbbsbbbs"},
    {54,	"bbbsbsbbsss"},
    {55,	"bbbsbsssbbs"},
    {56,	"bbbsssbsbbs"},
    {57,	"bbbsbbsbsss"},
    {58,	"bbbsbbsssbs"},
    {59,	"bbbsssbbsbs"},
    {60,	"bbbsbbbbsbs"},
    {61,	"bbssbssssbs"},
    {62,	"bbbbsssbsbs"},
    {63,	"bsbssbbssss"},
    {64,	"bsbssssbbss"},
    {65,	"bssbsbbssss"},
    {66,	"bssbssssbbs"},
    {67,	"bssssbsbbss"},
    {68,	"bssssbssbbs"},
    {69,	"bsbbssbssss"},
    {70,	"bsbbssssbss"},
    {71,	"bssbbsbssss"},
    {72,	"bssbbssssbs"},
    {73,	"bssssbbsbss"},
    {74,	"bssssbbssbs"},
    {75,	"bbssssbssbs"},
    {76,	"bbssbsbssss"},
    {77,	"bbbbsbbbsbs"},
    {78,	"bbssssbsbss"},
    {79,	"bsssbbbbsbs"},
    {80,	"bsbssbbbbss"},
    {81,	"bssbsbbbbss"},
    {82,	"bssbssbbbbs"},
    {83,	"bsbbbbssbss"},
    {84,	"bssbbbbsbss"},
    {85,	"bssbbbbssbs"},
    {86,	"bbbbsbssbss"},
    {87,	"bbbbssbsbss"},
    {88,	"bbbbssbssbs"},
    {89,	"bbsbbsbbbbs"},
    {90,	"bbsbbbbsbbs"},
    {91,	"bbbbsbbsbbs"},
    {92,	"bsbsbbbbsss"},
    {93,	"bsbsssbbbbs"},
    {94,	"bsssbsbbbbs"},
    {95,	"bsbbbbsbsss"},
    {96,	"bsbbbbsssbs"},
    {97,	"bbbbsbsbsss"},
    {98,	"bbbbsbsssbs"},
    {99,	"bsbbbsbbbbs"},
    {100,	"bsbbbbsbbbs"},
    {101,	"bbbsbsbbbbs"},
    {102,	"bbbbsbsbbbs"},
    {103,	"bbsbssssbss"},
    {104,	"bbsbssbssss"},
    {105,	"bbsbssbbbss"},
    {106,	"bbsssbbbsbsbb"},
};

Manage::Manage(QObject *parent,int height,int barwidth,int xwidth)
    : QObject(parent)
{
    m_xheight = height;
    m_barwidth = barwidth;
    m_xwidth = xwidth;
}

Manage::~Manage(){

}




































































bool Manage::savetobmp(QString brcode,QString path){

    QByteArray str = brcode.toUtf8();
    QByteArray startC = "bbsbssbbbss";

    QByteArray pInc;
    pInc.append(startC);

    QList<int> temp;
    temp.append(105);

    if(str.size()%2==0){

    }
    else{
        qDebug()<<"code error:even num only";
        return false;
    }

    for(int n=0;n<str.size();n+=2){
        int x = str.mid(n,2).toInt();
        if(x<107){
            temp.append(infs[x].ch);
            pInc.append(infs[x].psz);
        }
        else{
            qWarning()<<"Code index Out of size";
        }
    }

    int result = 105;

    for(int i=1;i<temp.size();i++){
        result = result + temp.value(i)*i;
    }
    result = (result%103);

    if(result<106){
        pInc.append(infs[result].psz);
    }
    else{
        qWarning()<<"default result:"<<result;
    }

    QByteArray stop = "bbsssbbbsbsbb";
    pInc.append(stop);

    QImage image(QSize(pInc.size()*m_barwidth+m_xwidth*2,m_xheight),QImage::Format_ARGB32);
    image.fill("white");
    QPainter *painter = new QPainter(&image);
    painter->save();
    int iPenW = m_barwidth;
    int x0 = m_xwidth,y0(0);
    int barheight = m_xheight;
    QPen hPenBar(Qt::black,iPenW,Qt::SolidLine);
    QPen hPenSpace(Qt::white,iPenW);

    for(int i=0;i<pInc.size();i++){
        if(pInc.at(i)=='b'){
            painter->setPen(hPenBar);
        }
        else{
            painter->setPen(hPenSpace);
        }
        painter->drawLine(x0,y0,x0,y0+barheight);
        x0+=iPenW;
    }

    painter->end();

    return image.save(path,"bmp");
}
